package cn.alibab.common.config.excpetion;

import cn.alibab.common.model.R;
import cn.alibab.common.model.RCode;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.yaml.snakeyaml.constructor.DuplicateKeyException;


/**
 * @author shiyk
 * @create 2020-07-14 15:22
 */
@RestControllerAdvice
public class RRExceptionHandler {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @ExceptionHandler(ServiceException.class)
    public R handlerServiceException(ServiceException e) {
        return R.error(e.getRCode());
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public R handlerNoFoundException(Exception e) {
        logger.error(e.getMessage(), e);
        return R.error(RCode.NOT_FOUND);
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public R handlerDuplicateKeyException(DuplicateKeyException e) {
        logger.error(e.getMessage(), e);
        return R.error(RCode.REPEAT_KEY);
    }

    @ExceptionHandler(AuthorizationException.class)
    public R handlerAuthorizationException(AuthorizationException e) {
        logger.error(e.getMessage(), e);
        return R.error(RCode.AUTHORZITION_ERROR);
    }
    @ExceptionHandler(Exception.class)
    public R handlerException(Exception e){
        logger.error(e.getMessage(), e);
        return R.error();
    }
}
